﻿using CreatingObject;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;

namespace Test.CreatingObject
{
    [TestClass()]
    public class CreatingObjectTest
    {
        [TestMethod()]
        [DeploymentItem("CreatingObject.exe")]
        public void ModifyCustomerTest()
        {
            NorthwindEntities nWEntity = new NorthwindEntities();
            string customerID = "23456";
            string companyName = "Brand New Company";
            string contactName = null;
            string contactTitle = null;
            string address = null;
            string city = null;
            string region = null;
            string postalCode = null;
            string country = null;
            string phone = null;
            string fax = null;
            try
            {
                CreatingObject_Accessor.InsertCustomer(nWEntity, customerID, companyName, contactName, contactTitle, address,
                    city, region, postalCode, country, phone, fax);
                contactName = "Stanislav";
                city = "Sofia";
                CreatingObject_Accessor.ModifyCustomer(nWEntity, contactName, city, customerID);
                Customer customer = new Customer()
                {
                    CustomerID = customerID,
                    CompanyName = companyName,
                    ContactName = contactName,
                    ContactTitle = contactTitle,
                    Address = address,
                    City = city,
                    Region = region,
                    PostalCode = postalCode,
                    Country = country,
                    Phone = phone,
                    Fax = fax
                };
                if (!CustomerExists(customer))
                {
                    Assert.Fail();
                }
            }
            finally
            {
                CreatingObject_Accessor.DeletingCustomer(nWEntity, customerID);
            }
        }

        [TestMethod()]
        [DeploymentItem("CreatingObject.exe")]
        public void InsertCustomerTest()
        {
            NorthwindEntities nWEntity = new NorthwindEntities();
            string customerID = "23456";
            string companyName = "Brand New Company";
            string contactName = null;
            string contactTitle = null;
            string address = null;
            string city = null;
            string region = null;
            string postalCode = null;
            string country = null;
            string phone = null;
            string fax = null;
            CreatingObject_Accessor.InsertCustomer(nWEntity, customerID, companyName, contactName, contactTitle, address,
                city, region, postalCode, country, phone, fax);
            Customer customer = new Customer()
            {
                CustomerID = customerID,
                CompanyName = companyName,
                ContactName = contactName,
                ContactTitle = contactTitle,
                Address = address,
                City = city,
                Region = region,
                PostalCode = postalCode,
                Country = country,
                Phone = phone,
                Fax = fax
            };
            try
            {
                if (!CustomerExists(customer))
                {
                    Assert.Fail();
                }
            }
            finally
            {
                Customer deleteCustomer = nWEntity.Customers.FirstOrDefault(c => c.CustomerID == customerID);
                nWEntity.Attach(deleteCustomer);
                nWEntity.Customers.DeleteObject(deleteCustomer);
                nWEntity.SaveChanges();
            }
        }

        private bool CustomerExists(Customer customer)
        {
            NorthwindEntities nWEntity = new NorthwindEntities();
            var customers =
                from c in nWEntity.Customers
                where c.CustomerID == customer.CustomerID
                select c;
            foreach (var item in customers)
            {
                bool exists = (item.CustomerID == customer.CustomerID) && (item.ContactName == customer.ContactName)
                    && (item.City == customer.City);
                if (exists)
                {
                    return true;
                }
            }
            return false;
        }

        [TestMethod()]
        [DeploymentItem("CreatingObject.exe")]
        public void DeletingCustomerTest()
        {
            NorthwindEntities nWEntity = new NorthwindEntities();
            string customerID = "23456";
            string companyName = "Brand New Company";
            string contactName = null;
            string contactTitle = null;
            string address = null;
            string city = null;
            string region = null;
            string postalCode = null;
            string country = null;
            string phone = null;
            string fax = null;
            CreatingObject_Accessor.InsertCustomer(nWEntity, customerID, companyName, contactName, contactTitle, address,
                city, region, postalCode, country, phone, fax);
            CreatingObject_Accessor.DeletingCustomer(nWEntity, customerID);
            Customer customer = new Customer()
            {
                CustomerID = customerID,
                CompanyName = companyName,
                ContactName = contactName,
                ContactTitle = contactTitle,
                Address = address,
                City = city,
                Region = region,
                PostalCode = postalCode,
                Country = country,
                Phone = phone,
                Fax = fax
            };
            if (CustomerExists(customer))
            {
                Assert.Fail();
            }
        }
    }
}
